/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.datetime;

import org.openaecfm.model.measure.Boolean;
import org.openaecfm.model.datetime.DateTime;
import org.openaecfm.model.datetime.Duration;
import org.openaecfm.model.datetime.SchedulingTime;
import org.openaecfm.model.datetime.TaskDurationEnum;
import org.openaecfm.model.datetime.TaskTime;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class TaskTimeIO extends AbstractEntityInstanceInputImpl<TaskTime>
		implements IEntityInstanceOutput<TaskTime>,
		IEntityInstanceUtilOutput<TaskTime>, IEntityInstanceUtilInput<TaskTime> {

	private IEntityInstanceUtilOutput<SchedulingTime> utilOutput;
	private IEntityInstanceUtilInput<SchedulingTime> utilInput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<SchedulingTime> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<SchedulingTime> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public void exportSPF(TaskTime pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		utilOutput.exportSPF(pojo, ei, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set DurationType
		if (pojo.getDurationType() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EnumerationParameter(pojo.getDurationType()));
		}

		// Set ScheduleDuration
		if (pojo.getScheduleDuration() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getScheduleDuration()
					.toString(), StringParameter.UTF_8));
		}

		// Set ScheduleStart
		if (pojo.getScheduleStart() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getScheduleStart()
					.toString(), StringParameter.UTF_8));
		}

		// Set ScheduleFinish
		if (pojo.getScheduleFinish() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getActualFinish()
					.toString(), StringParameter.UTF_8));
		}

		// Set EarlyStart
		if (pojo.getEarlyStart() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getEarlyStart()
					.toString(), StringParameter.UTF_8));
		}

		// Set EarlyFinish
		if (pojo.getEarlyFinish() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getEarlyFinish()
					.toString(), StringParameter.UTF_8));
		}

		// Set LateStart
		if (pojo.getLateStart() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(
					pojo.getLateStart().toString(), StringParameter.UTF_8));
		}

		// Set LateFinish
		if (pojo.getLateFinish() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getLateFinish()
					.toString(), StringParameter.UTF_8));
		}

		// Set FreeFloat
		if (pojo.getFreeFloat() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(
					pojo.getFreeFloat().toString(), StringParameter.UTF_8));
		}

		// Set TotalFloat
		if (pojo.getTotalFloat() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getTotalFloat()
					.toString(), StringParameter.UTF_8));
		}

		// Set IsCritical
		if (pojo.getIsCritical() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EnumerationParameter(pojo.getIsCritical()));
		}

		// Set StatusTime
		if (pojo.getStatusTime() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getStatusTime()
					.toString(), StringParameter.UTF_8));
		}

		// Set ActualDuration
		if (pojo.getActualDuration() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getActualDuration()
					.toString(), StringParameter.UTF_8));
		}

		// Set ActualStart
		if (pojo.getActualStart() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getActualStart()
					.toString(), StringParameter.UTF_8));
		}

		// Set ActualFinish
		if (pojo.getActualFinish() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getActualFinish()
					.toString(), StringParameter.UTF_8));
		}

		// Set RemainingTime
		if (pojo.getRemainingTime() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getRemainingTime()
					.toString(), StringParameter.UTF_8));
		}

		// Set Completion
		if (pojo.getCompletion() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getCompletion()));
		}
	}

	@Override
	public String exportSPF(TaskTime pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCTASKTIME",
				appDataManager.getName(pojo));

		exportSPF(pojo, ei, appDataManager);

		return ei.toString();
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei, TaskTime pojo,
			IAppDataManager appDataManager) {
		return;
	}

	@Override
	protected TaskTime doCreateObject() {
		return new TaskTime();
	}

	@Override
	public void resolveNotReferenceParameter(EntityInstance ei, TaskTime pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set DurationType
		Parameter durationType = sei.getParameter(3);
		if (durationType.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) durationType;
			pojo.setDurationType(TaskDurationEnum.valueOf(e.toEnumString()));
		}

		// Set ScheduleDuration
		Parameter scheduleDuration = sei.getParameter(4);
		if (scheduleDuration.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) scheduleDuration;
			pojo.setScheduleDuration(new Duration(s.toUTF8()));
		}

		// Set ScheduleStart
		Parameter scheduleStart = sei.getParameter(5);
		if (scheduleStart.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) scheduleStart;
			pojo.setScheduleStart(new DateTime(s.toUTF8()));
		}

		// Set ScheduleFinish
		Parameter scheduleFinish = sei.getParameter(6);
		if (scheduleFinish.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) scheduleFinish;
			pojo.setScheduleFinish(new DateTime(s.toUTF8()));
		}

		// Set EarlyStart
		Parameter earlyStart = sei.getParameter(7);
		if (earlyStart.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) earlyStart;
			pojo.setEarlyStart(new DateTime(s.toUTF8()));
		}

		// Set EarlyFinish
		Parameter earlyFinish = sei.getParameter(8);
		if (earlyFinish.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) earlyFinish;
			pojo.setEarlyFinish(new DateTime(s.toUTF8()));
		}

		// Set LateStart
		Parameter lateStart = sei.getParameter(9);
		if (lateStart.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) lateStart;
			pojo.setLateStart(new DateTime(s.toUTF8()));
		}

		// Set LateFinish
		Parameter lateFinish = sei.getParameter(10);
		if (lateFinish.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) lateFinish;
			pojo.setLateFinish(new DateTime(s.toUTF8()));
		}

		// Set FreeFloat
		Parameter freeFloat = sei.getParameter(11);
		if (freeFloat.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) freeFloat;
			pojo.setFreeFloat(new Duration(s.toUTF8()));
		}

		// Set TotalFloat
		Parameter totalFloat = sei.getParameter(12);
		if (totalFloat.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) totalFloat;
			pojo.setTotalFloat(new Duration(s.toUTF8()));
		}

		// Set IsCritical
		Parameter isCritical = sei.getParameter(13);
		if (isCritical.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) isCritical;
			pojo.setIsCritical(Boolean.valueOf(e.toEnumString()));
		}

		// Set StatusTime
		Parameter statusTime = sei.getParameter(14);
		if (statusTime.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) statusTime;
			pojo.setStatusTime(new DateTime(s.toUTF8()));
		}

		// Set ActualDuration
		Parameter actualDuration = sei.getParameter(15);
		if (actualDuration.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) actualDuration;
			pojo.setActualDuration(new Duration(s.toUTF8()));
		}

		// Set ActualStart
		Parameter actualStart = sei.getParameter(16);
		if (actualStart.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) actualStart;
			pojo.setActualStart(new DateTime(s.toUTF8()));
		}

		// Set ActualFinish
		Parameter actualFinish = sei.getParameter(17);
		if (actualFinish.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) actualFinish;
			pojo.setActualFinish(new DateTime(s.toUTF8()));
		}

		// Set RemainingTime
		Parameter remainingTime = sei.getParameter(18);
		if (remainingTime.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) remainingTime;
			pojo.setRemainingTime(new Duration(s.toUTF8()));
		}

		// Set Completion
		Parameter completion = sei.getParameter(19);
		if (completion.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) completion;
			pojo.setCompletion(r.toDouble());
		}
	}
}
